package com.booktrade.config;


import com.booktrade.interceptor.JwtTokenUserInterceptor;
import com.booktrade.util.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {


    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
//        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/users/**")
                .addPathPatterns("/books/update")
                .addPathPatterns("/books/publish")
                .addPathPatterns("/books/user")
                .addPathPatterns("/collection/**")
                .addPathPatterns("/notifications/list")
                .addPathPatterns("/reviews/**")
                .addPathPatterns("/cart/**")
                .addPathPatterns("/orders/**")
                .addPathPatterns("/messages/**")
                .excludePathPatterns("/users/login")
                .excludePathPatterns("/users/register");
    }


    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        // 配置静态资源路径，映射 /static/** 到 classpath:/static/
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //TODO 可以使用一个本地绝对路径，在项目目录下之前的图片会访问不到
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:" + System.getProperty("user.dir") + "/uploads/");  // 本地目录，必须以 "file:" 前缀开头


    }


    /**
     * 扩展Spring MVC框架的消息转化器
     *
     * @param converters
     */
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器...");
        //创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //需要为消息转换器设置一个对象转化器，对象转换器可以将Java对象序列化为json属性
        converter.setObjectMapper(new JacksonObjectMapper());
        //将自己的消息转换器加入到mvc框架的转换器集合中
        converters.add(1, converter);
    }

}
